package de.dhbw.karlsruhe.picsim.gui;

import de.dhbw.karlsruhe.picsim.gui.data.SwingStackElementImpl;
import de.dhbw.karlsruhe.picsim.model.memory.CommandRegisterAddress;
import de.dhbw.karlsruhe.picsim.model.memory.DirectAddressAbleStack;
import de.dhbw.karlsruhe.picsim.model.memory.impl.StackFacadeImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jetbrains.annotations.Nullable;

import javax.swing.table.AbstractTableModel;

/**
 * Created by IntelliJ IDEA.
 * User: goesen
 * Date: Apr 29, 2010
 * Time: 12:00:08 PM
 */
public final class SwingStack extends AbstractTableModel {
    private final DirectAddressAbleStack stackFacade;
    private static final Log log = LogFactory.getLog(SwingStack.class);

    public SwingStack(final DirectAddressAbleStack stackFacade) {
        this.stackFacade = stackFacade;
    }

    @Override
    public int getRowCount() {
        return StackFacadeImpl.HIGHEST_STACK_NUMBER + 1;
    }

    @Override
    public int getColumnCount() {
        return 1;
    }

    @Override
    public void setValueAt(@Nullable final Object aValue, final int rowIndex, final int columnIndex) {
        if (aValue != null) {
            stackFacade.setAddress(rowIndex, (CommandRegisterAddress) aValue);
            fireTableCellUpdated(rowIndex, columnIndex);
        } else {
            log.info("User input was discarded at " + rowIndex);
        }

    }


    @SuppressWarnings({"RefusedBequest"})
    @Override
    public boolean isCellEditable(final int rowIndex, final int columnIndex) {
        return true;
    }

    @Override
    public Object getValueAt(final int rowIndex, final int columnIndex) {
        return new SwingStackElementImpl(stackFacade, rowIndex);
    }

    @Override
    public String getColumnName(final int column) {
        return "Stack";
    }
}
